package model.dao.hosotiemchung;

import java.util.Date;
import java.util.List;
import model.dao.AbstractQuery;
import model.dao.ProjectQuery;
import model.dao.phieutiemchung.PhieuTiemChungDAO;
import model.dao.vatnuoi.VatNuoiDAO;
import model.entity.HoSoTiemChung;
import model.entity.VatNuoi;
import model.initialize.Initialize;
import model.util.HibernateUtil;
import org.hibernate.Session;

public class HoSoTiemChungDAO {
      public static boolean themHoSoTiemChung(int maVatNuoi, String tenBenh, Date ngayBatDau, Date ngayKetThuc, String chuThich){
  
        VatNuoi vatNuoi = VatNuoiDAO.timTheoMaVatNuoi(maVatNuoi) ;
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.getTransaction().begin();
        if(vatNuoi == null){
            session.getTransaction().commit();
            return false;
        }else{
            HoSoTiemChung hoSo = new HoSoTiemChung(tenBenh, vatNuoi, ngayBatDau, ngayKetThuc, "Chưa kết thúc", chuThich, null);
            session.save(hoSo);
            session.getTransaction().commit();
            return  true;
        }
    }
      public static boolean suaHoSoTiemChung(int maHoSo, int maVatNuoi, String tenBenh, String trangThai, Date ngayBatDau, Date ngayKetThuc, String ghiChu){
          HoSoTiemChung hoSoTiemChung = timTheoMaHoSo(maHoSo).get(0);
          VatNuoi vatNuoi = VatNuoiDAO.timTheoMaVatNuoi(maVatNuoi);
          
          Session session = HibernateUtil.getSessionFactory().getCurrentSession();
          session.getTransaction().begin();
          if(hoSoTiemChung != null){
              if(vatNuoi != null){
                  hoSoTiemChung.setVatNuoi(vatNuoi);
                  hoSoTiemChung.setBenh(tenBenh);
                  hoSoTiemChung.setHstcTrangThai(trangThai);
                  hoSoTiemChung.setHstcNgayBd(ngayBatDau);
                  hoSoTiemChung.setHstcNgayKt(ngayKetThuc);
                  hoSoTiemChung.setHstcGhiChu(ghiChu);
                  session.update(hoSoTiemChung);
                  session.getTransaction().commit();
                  return true;
              }
              
          }
          session.getTransaction().commit();
          return false;
          
      }
      public static boolean suaTrangThaiHoSo(int maHoSo){
          HoSoTiemChung hoSoTiemChung = HoSoTiemChungDAO.timTheoMaHoSo_HoSo(maHoSo);
          Session session = null;
         if(hoSoTiemChung != null){
            if(hoSoTiemChung.getHstcTrangThai().equalsIgnoreCase("Chưa kết thúc")){
                hoSoTiemChung.setHstcTrangThai("Kết thúc");
            }else{
                hoSoTiemChung.setHstcTrangThai("Chưa kết thúc");
            }
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.getTransaction().begin();
            session.update(hoSoTiemChung);
            session.getTransaction().commit();
            return true;
         }
         session.getTransaction().commit();
         return false;
        
      }
    public static boolean xoaHoSoTiemChung(int maHoSo){
         HoSoTiemChung hoSoTiemChung = timTheoMaHoSo(maHoSo).get(0);
         Session session = null;
         if(hoSoTiemChung != null){
             PhieuTiemChungDAO.xoaPTCTheoMaHoSo(maHoSo);
              session = HibernateUtil.getSessionFactory().getCurrentSession();
             
             try{
             session.getTransaction().begin();
             
             session.delete(hoSoTiemChung);
             
             session.getTransaction().commit();
             return true;
             }catch(Exception e){
                 e.printStackTrace();
                 return false;
             }
         
         }
         session.getTransaction().commit();
         return false;
         
         
    }
    /**
     * Load tất cả hồ sơ tiêm chủng vào 1 List
     *
     * @return List danh sách tất cả các hồ sơ tiêm chủng. Mỗi 1 đối tượng trong List
     * trả về là 1 dòng (row) trong database.
     */
    public static final List<HoSoTiemChung> danhSachHoSoTiemChung() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.getTransaction().begin();
        List<HoSoTiemChung> list = session.createQuery("FROM HoSoTiemChung as hoSoTiemChung").list();
        for (HoSoTiemChung hoSoTiemChung : list) {
            Initialize.initializeHoSoTiemChung(hoSoTiemChung);
        }
        session.getTransaction().commit();
        return list;
    }

    public static final List<HoSoTiemChung> timTheoMaHoSo(int maHoSo) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.getTransaction().begin();
        List<HoSoTiemChung> list = session.createQuery("FROM HoSoTiemChung AS hoSoTiemChung WHERE hoSoTiemChung.hstcMa = ?").setInteger(0, maHoSo).list();
        for (HoSoTiemChung hoSoTiemChung : list) {
            Initialize.initializeHoSoTiemChung(hoSoTiemChung);
        }
        session.getTransaction().commit();
        return list;
    }
    public static final HoSoTiemChung timTheoMaHoSo_HoSo(int maHoSo) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.getTransaction().begin();
        List<HoSoTiemChung> list = session.createQuery("FROM HoSoTiemChung AS hoSoTiemChung WHERE hoSoTiemChung.hstcMa = ?").setInteger(0, maHoSo).list();
        for (HoSoTiemChung hoSoTiemChung : list) {
            Initialize.initializeHoSoTiemChung(hoSoTiemChung);
        }
        session.getTransaction().commit();
        if(list.size() > 0)
            return  list.get(0);
        return null;
    }
    public static final List<HoSoTiemChung> timHoSoTheoTenBenh(String tenBenh) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.getTransaction().begin();
        List<HoSoTiemChung> list = session.createQuery("FROM HoSoTiemChung AS hoSoTiemChung WHERE hoSoTiemChung.benh = ?").setString(0, tenBenh).list();
        for (HoSoTiemChung hoSoTiemChung : list) {
            Initialize.initializeHoSoTiemChung(hoSoTiemChung);
        }
        session.getTransaction().commit();
        return list;
    }
    public static List<HoSoTiemChung> timHoSoTheoMaVatNuoi(int maVatNuoi){
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.getTransaction().begin();
        List<HoSoTiemChung> list = session.createQuery("FROM HoSoTiemChung AS hoSoTiemChung WHERE hoSoTiemChung.vatNuoi.vnMa = ?").setInteger(0, maVatNuoi).list();
        for (HoSoTiemChung hoSoTiemChung : list) {
            Initialize.initializeHoSoTiemChung(hoSoTiemChung);
        }
        return list;
    }
    public static void main(String[] args){
//        List<HoSoTiemChung> list  = timHoSoTheoMaVatNuoi(1);
//        System.out.println(list);
//        boolean result = suaHoSoTiemChung(13, 1, "Sốt", "Kết thúc", new Date(), new Date(), "Không ra gió, uống nhiều nước.");
//        System.out.println(result);
//            boolean result = suaTrangThaiHoSo(13);
//            System.out.println(result);
        
    }

    /**
     * Thêm mới 1 hồ sơ tiêm chủng
     *
     * @param hstc đối tượng thuốc cần thêm
     */
    public final static boolean insertHSTC(HoSoTiemChung hstc) {
        AbstractQuery.insertObject(hstc.getVatNuoi());
        AbstractQuery.insertObject(hstc);
        return true;
    }

    /**
     * Sửa thông tin 1 hồ sơ tiêm chủng.
     *
     * @param hstc đối tượng thuốc sẽ được sửa thông tin.
     */
    public static final boolean updateHSTC(HoSoTiemChung hstc) {
        AbstractQuery.updateObject(hstc);
        return true;
    }

    /**
     * Xóa 1 hồ sơ tiêm chủng.
     *
     * @param hstcMa mã đối tượng thuốc sẽ bị xóa.
     */
    public static final void deleteHSTC(int hstcMa) {
        AbstractQuery.modifyObject(3, (HoSoTiemChung) AbstractQuery.loadObject(ProjectQuery.LOAD_1_HSTC + hstcMa).get(0));
    }
}
